#include <iostream>
#include <conio.h> // 用于_getch()
#include <windows.h> // 用于Sleep()和清屏
#include <vector>
#include <ctime> // 用于随机数种子

using namespace std;

bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
vector<pair<int, int>> snake; // 蛇的身体坐标
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;

void Setup() {
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    snake.clear();
    snake.push_back({ x, y }); // 初始蛇头

    // 随机生成水果位置
    srand(time(0));
    fruitX = rand() % width;
    fruitY = rand() % height;

    score = 0;
}

void Draw() {
    system("cls"); // 清屏

    // 绘制上边界
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (j == 0)
                cout << "#"; // 左边界

            // 绘制蛇头
            if (i == y && j == x)
                cout << "O";
            // 绘制蛇身
            else if (find(snake.begin() + 1, snake.end(), make_pair(j, i)) != snake.end())
                cout << "o";
            // 绘制水果
            else if (i == fruitY && j == fruitX)
                cout << "F";
            else
                cout << " ";

            if (j == width - 1)
                cout << "#"; // 右边界
        }
        cout << endl;
    }

    // 绘制下边界
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

    cout << "Score: " << score << endl;
}

void Input() {
    if (_kbhit()) {
        switch (_getch()) {
        case 'a':
            if (dir != RIGHT) // 防止直接反向移动
                dir = LEFT;
            break;
        case 'd':
            if (dir != LEFT)
                dir = RIGHT;
            break;
        case 'w':
            if (dir != DOWN)
                dir = UP;
            break;
        case 's':
            if (dir != UP)
                dir = DOWN;
            break;
        case 'x':
        case 'X':
            gameOver = true;
            break;
        }
    }
}

void Logic() {
    // 保存蛇尾位置用于可能的增长
    pair<int, int> prevTail = snake.back();

    // 移动蛇身（从尾部开始更新位置）
    for (size_t i = snake.size() - 1; i > 0; i--) {
        snake[i] = snake[i - 1];
    }

    // 根据方向移动蛇头
    switch (dir) {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    default:
        break;
    }

    // 更新蛇头位置
    snake[0] = { x, y };

    // 检查是否吃到水果
    if (x == fruitX && y == fruitY) {
        score += 10;
        // 蛇身增长（添加原来的尾部位置）
        snake.push_back(prevTail);

        // 生成新的水果位置
        fruitX = rand() % width;
        fruitY = rand() % height;
    }

    // 检查是否撞墙
    if (x >= width || x < 0 || y >= height || y < 0)
        gameOver = true;

    // 检查是否撞到自己
    for (size_t i = 1; i < snake.size(); i++) {
        if (snake[i].first == x && snake[i].second == y)
            gameOver = true;
    }
}

int main() {
    cout << "选择游戏难度：A.快速 B.中速 C.慢速\n";
    char ch;cin >> ch;
    int tim;
    if (ch == 'A') tim = 50;
    if (ch == 'B') tim = 100;
    if (ch == 'C') tim = 200;
    Setup();
    while (!gameOver) {
        Draw();
        Input();
        Logic();
        Sleep(tim); // 控制游戏速度
    }
    cout << "Game Over! Final Score: " << score << endl;
    system("pause");
    return 0;
}